﻿
using RTW.DAL;
using RTW.OA.Models;
using RTW.Utils.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace RTW.OA.Areas.System.Servers
{
    public class TemplateServers
    {
        private readonly IRepository<template> templateRepository;

        public TemplateServers()
        {
            templateRepository = new DapperRepository<template>();
        }

        public List<template> GetData()
        {
            return templateRepository.GetAllEnumerable().ToList();
        }

        public template GetModelById(string id)
        {
            return templateRepository.GetById(id);
        }


        internal void Add(template model)
        {
            templateRepository.InsertUpdateOrDeleteSql("insert into [dbo].[template] values(@SORT_ID,@PRIV_STR,@PRIV_MODULE,GETDATE(),@USER_ID,@MODEL_NAME,@ATTACHMENT,@DESC,@TYPE)",
                new { SORT_ID = model.SORT_ID, PRIV_STR = model.PRIV_STR, PRIV_MODULE = model.PRIV_MODULE, USER_ID = model.USER_ID, MODEL_NAME = model.MODEL_NAME, ATTACHMENT = model.ATTACHMENT, DESC = model.DESC, TYPE = model.TYPE });
        }

        internal void Update(template model)
        {
            templateRepository.InsertUpdateOrDeleteSql("update [dbo].[template] set SORT_ID=@SORT_ID,PRIV_STR=@PRIV_STR,PRIV_MODULE=@PRIV_MODULE,CREATE_TIME=GETDATE(),[USER_ID]=@USER_ID,MODEL_NAME=@MODEL_NAME,ATTACHMENT=@ATTACHMENT,[DESC]=@DESC,[TYPE]=@TYPE where DOC_ID=@DOC_ID",
                new { DOC_ID = model.DOC_ID, SORT_ID = model.SORT_ID, PRIV_STR = model.PRIV_STR, PRIV_MODULE = model.PRIV_MODULE, USER_ID = model.USER_ID, MODEL_NAME = model.MODEL_NAME, ATTACHMENT = model.ATTACHMENT, DESC = model.DESC, TYPE = model.TYPE });
        }

        internal void Del(string id)
        {
            templateRepository.DeleteByIds(id);
        }
        
        internal List<SelectListItem> GetTemplateKind()
        {
            List<SelectListItem> tables = new List<SelectListItem>();
            var dstable = templateRepository.SqlWithParamsDynamic("select CODE_NO Value,concat(CODE_NO,'-----',CODE_NAME) Text  from [dbo].[sys_code] where PARENT_NO=171");
            tables.Clear();

            foreach (var dr in dstable)
            {
                string Id = dr.Value.ToString();
                string Title = dr.Text.ToString().Trim();
                tables.Add(new SelectListItem { Value = Id, Text = Title });
            }

            return tables;
        }
        
    }
}